package gam.n1.conf.i;


import gam.n1.areas.Area;
import gam.n1.areas.AreaManager;
import gam.n1.areas.block.BlockSpace;
import gam.n1.conf.BaseConfig;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

// 文件名格式： [UUID].json
public class AreaConfig extends BaseConfig {


    public AreaConfig(@NotNull Plugin plugin, @NotNull String filePath) {
        super(plugin, filePath);
    }

    public void save(Area area) throws IOException {
        Map<String, Object> save = area.save();
        save.put("uuid",area.getAreaUUID().toString());
        String text = gson.toJson(save);
        Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{
            try {
                File file = new File(area.plugin().getDataFolder(),fP + "/" + area.getAreaUUID().toString().replace("-","") + ".json");
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileWriter fw = new FileWriter(file);
                fw.write(text);
                fw.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        });
    }

    @Override
    public boolean load(Plugin p) throws Exception {
        File var1 = getBaseFile(p);
        File[] var2 = var1.listFiles();
        if (var2 != null && var2.length > 0){
            for (File file : var2) {
                String var4 = file.getName().substring(file.getName().indexOf("."));
                if (var4.equals(".json")){
                    StringBuilder var6 = new StringBuilder();
                    try {
                        int i ;
                        FileInputStream fi = new FileInputStream(file);
                        while ((i = fi.read()) != -1) var6.append((char) i);
                        fi.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                    Area area = handleMapToArea(gson.fromJson(var6.toString(), Map.class));
                    if (area != null){
                        AreaManager.addArea(area);
                    }
                }
            }
        }
        return true;
    }
    private Area handleMapToArea(Map m) throws Exception {
        Objects.requireNonNull(m);
        String uuid = (String) m.get("uuid");
        String name = (String) m.get("name");
        String displayName = (String) m.get("displayName");
        String type = (String) m.get("type");
        Map box = (Map) m.get("box");
        Area area = AreaManager.Build(plugin, type, name);
        if (area != null){
            area.setAreaDisplayName(displayName);
            area.setAreaUUID(UUID.fromString(uuid));
            area.setBox(BlockSpace.m(box));
            Area temp;
            if ((temp = pC(area,m)) != null){
                area = temp;
            }
        }
        return area;
    }
    protected Area pC(Area area,Map map){
        return null;
    }



    @Override
    public boolean reload(Plugin p) throws Exception {
        return load(p);
    }
}
